package com.xlh.support.quiz.dao.helper;

import com.xlh.support.quiz.dao.QuizMapper;
import com.xlh.support.quiz.domain.Quiz;
import com.xlh.support.quiz.domain.example.QuizExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cheer
 */
@Repository
public class QuizMapperHelper {

    @Autowired
    private QuizMapper quizMapper;

    public void deleteById(Long id) {
        Quiz quiz = Quiz.builder()
                .id(id)
                .deleted(true)
                .build();
        quizMapper.updateByPrimaryKeySelective(quiz);
    }

    // 真删
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            QuizExample example = new QuizExample();
            example.or().andIdIn(ids);
            quizMapper.deleteByExample(example);
        }
    }

    public void updateQuizNumber(Long quizId, Integer quizNumber) {
        Quiz quiz = Quiz.builder()
                .id(quizId)
                .quizNumber(quizNumber)
                .build();
        quizMapper.updateByPrimaryKeySelective(quiz);
    }

    public Quiz getById(Long id) {
        Quiz quiz = quizMapper.selectByPrimaryKey(id);
        if (quiz == null || quiz.getDeleted()) {
            return null;
        }
        return quiz;
    }

    public List<Quiz> listQuiz(Long chapterId, Integer pageNumber) {
        QuizExample example = new QuizExample();
        example.or().andChapterIdEqualTo(chapterId)
                .andPageNumberEqualTo(pageNumber)
                .andDeletedEqualTo(false);
        return quizMapper.selectByExample(example);
    }

    public List<Quiz> listQuizByChapterIds(List<Long> chapterIds) {
        if (CollectionUtils.isEmpty(chapterIds)) {
            return new ArrayList<>();
        }

        QuizExample example = new QuizExample();
        example.or().andChapterIdIn(chapterIds);
        return quizMapper.selectByExample(example);
    }
}
